#include "threados.hpp"

using namespace systemos;

/*-----------------------------------------------------------*/
extern "C"
{
	void vApplicationMallocFailedHook( void )
	{
		/* vApplicationMallocFailedHook() will only be called if
		configUSE_MALLOC_FAILED_HOOK is set to 1 in FreeRTOSConfig.h.  It is a hook
		function that will get called if a call to pvPortMalloc() fails.
		pvPortMalloc() is called internally by the kernel whenever a task, queue,
		timer or semaphore is created.  It is also called by various parts of the
		demo application.  If heap_1.c or heap_2.c are used, then the size of the
		heap available to pvPortMalloc() is defined by configTOTAL_HEAP_SIZE in
		FreeRTOSConfig.h, and the xPortGetFreeHeapSize() API function can be used
		to query the size of free heap space that remains (although it does not
		provide information on how the remaining heap might be fragmented). */
		taskDISABLE_INTERRUPTS();
		for( ;; );
	}
	
	/*-----------------------------------------------------------*/
	void vApplicationIdleHook( void )
	{
		/* vApplicationIdleHook() will only be called if configUSE_IDLE_HOOK is set
		to 1 in FreeRTOSConfig.h.  It will be called on each iteration of the idle
		task.  It is essential that code added to this hook function never attempts
		to block in any way (for example, call xQueueReceive() with a block time
		specified, or call vTaskDelay()).  If the application makes use of the
		vTaskDelete() API function (as this demo application does) then it is also
		important that vApplicationIdleHook() is permitted to return to its calling
		function, because it is the responsibility of the idle task to clean up
		memory allocated by the kernel to any task that has since been deleted. */
	}
	
	/*-----------------------------------------------------------*/
	void vApplicationStackOverflowHook( TaskHandle_t pxTask, char *pcTaskName )
	{
		( void ) pcTaskName;
		( void ) pxTask;

		/* Run time stack overflow checking is performed if
		configCHECK_FOR_STACK_OVERFLOW is defined to 1 or 2.  This hook
		function is called if a stack overflow is detected. */
		taskDISABLE_INTERRUPTS();
		//__BKPT();
		//  printf("Stack overflow task name=%s\n", pcTaskName);
		for( ;; );
	}
	
	/*-----------------------------------------------------------*/
	void vApplicationTickHook( void )
	{
		
	}
}

void threados::AppStart(void)
{
	vTaskStartScheduler();
}

/** 
* @brief      构造函数
* @param[in]  tTaskFunction 线程
* @retval    	 None
*/
threados::threados(TaskFunction_t tTaskFunction)
{
	mTaskFunction = tTaskFunction;
	mTaskHandle = 0;
	Name = 0;
	StackDeep = 256;
	Bind = 0;
	Prio = 0;
}

/** 
* @brief      析构函数
* @retval     None
*/
threados::~threados(void)
{
	Delete();
}

/** 
* @brief      删除任务
* @retval    	 None
*/
void threados::Delete(void)
{
	if(mTaskHandle)
	{
		vTaskDelete(mTaskHandle);
	}
}

/** 
* @brief      启动任务
* @retval    	 启动结果
*/
bool threados::Start(void)
{
	if(mTaskHandle)
	{
		vTaskResume(mTaskHandle);
		return(true);
	}
	else
	{
		if(xTaskCreate(mTaskFunction, Name, StackDeep, Bind, Prio, &mTaskHandle) == pdPASS)
		{
			return(true);
		}
		else
		{
			mTaskHandle = 0;
			return(false);
		}
	}
}

/** 
* @brief      恢复任务
* @retval    	 操作结果
*/
bool threados::Resume(void)
{
	if(mTaskHandle)
	{
		vTaskResume(mTaskHandle);
		return(true);
	}
	return(false);
}

/** 
* @brief      暂停任务
* @retval    	 操作结果
*/
bool threados::Pause(void)
{	
	if(mTaskHandle)
	{
		vTaskSuspend(mTaskHandle);
		return(true);
	}
	return(false);
}

